package com.sg.service.biz.bff.nb;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.sg.common.util.RedisUtil;
import com.sg.dto.integration.framework.FrameworkSceneRoleDto;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrframework.common.redis.BaseUserInfoForRealRedis;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
@BanAuto
public class NbEvaSystem {
    /**
     * code:getUserRoleListFromRedis
     * name:D1查询用户角色缓存Redis列表（特殊方法）
     * desc:undefined
     **/
    @Resource
    private RedisUtil redisClient;

    public QueryUserRoleRedisListRespDto queryUserRoleRedisList(QueryUserRoleRedisListReqDto reqDto) {
        QueryUserRoleRedisListRespDto retData = new QueryUserRoleRedisListRespDto();
        if (redisClient != null && reqDto.getOriginalRoleMemberId() != null) {
            GetUserBasicInfoFromRedisReqDto reqParam = new GetUserBasicInfoFromRedisReqDto();
            reqParam.setOriginalRoleMemberId(reqDto.getOriginalRoleMemberId());
            GetUserBasicInfoFromRedisRespDto userBaseInfo = getUserBasicInfoFromRedis(reqParam);
            List<FrameworkSceneRoleDto> roleList = userBaseInfo.getRoleList();
            retData.setRoleList(roleList.stream().map(role -> role.getRoleId()).collect(Collectors.toList()));
            return retData;
        }
        return retData;
    }

    @BanAuto
    public GetUserBasicInfoFromRedisRespDto getUserBasicInfoFromRedis(GetUserBasicInfoFromRedisReqDto reqDto) {
        GetUserBasicInfoFromRedisRespDto retData = new GetUserBasicInfoFromRedisRespDto();
        String str = (String) redisClient.get("userbase-" + reqDto.getOriginalRoleMemberId());
        if (str == null || str.isEmpty()) {
            return retData;
        }
        BaseUserInfoForRealRedis redisData = JSONObject.parseObject(str, BaseUserInfoForRealRedis.class);
        BeanUtil.copyProperties(redisData, retData, "birthday");
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            if (redisData.getBirthday() != null && !redisData.getBirthday().isEmpty()) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String times = sdf.format(Long.valueOf(redisData.getBirthday()));
                date = ft.parse(times);
                retData.setBirthday(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return retData;
    }

    /**
     * code:getUserRoleListFromRedis
     * name:D1查询用户角色缓存Redis列表（特殊方法）(公共)
     * desc:undefined
     **/
    @Trace(operationName = "D1查询用户角色缓存Redis列表（特殊方法）(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryUserRoleRedisListComRespDto queryUserRoleRedisListCom(QueryUserRoleRedisListComReqDto reqDto) {
        // TODO ruizhe skai dong ; 2022/6/25 下午4:51:20
        return new QueryUserRoleRedisListComRespDto();
    }

    /**
     * code:columnToRow
     * name:D数据集转字段（转换目标分类结果）(特殊方法)
     * desc:undefined
     * gen by moon at 7/5/2023, 1:36:57 AM
     **/
    @Trace(operationName = "D数据集转字段（转换目标分类结果）(特殊方法)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public TransitionColumnToRowRespDto transitionColumnToRow(TransitionColumnToRowReqDto reqDto) {
        // TODO ruizhe skai dong ; 7/5/2023, 1:36:57 AM
        return new TransitionColumnToRowRespDto();
    }
    //手工接入方法
}
